<?php 
	$pid = Yii::app()->request->getParam('pid');
?>
<script type="text/javascript" src="<?php echo Yii::app()->theme->baseUrl; ?>/js/jquery.validate.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
		$("#riskAddPopup").hide();
		$(".riskControl").hide();
		$("#riskEditPopup").hide();
		$(".riskContent").mouseover(function() {
			$(this).children(".riskControl").show();
			});
		$(".riskContent").mouseout(function() {
			$(".riskControl").hide();
			});
		$(".popupCancel").click(function() {
			$.fancybox.close();
		});
		//For editing risk entry
		//Display when ready
		var value = $("#riskEdit #selectProbability").val();
		if(value <= "3" && value != "10" ) {
			$("#riskEdit #probabilityLabel").text("Low probability");
		} 
		else if(value > "4" && value <= "6") {
			$("#riskEdit #probabilityLabel").text("Medium probability");
			}
		else 
			$("#riskEdit #probabilityLabel").text("High probability");

		//Display when change
		$("#riskEdit #selectProbability").change(function() {
			$("#riskEdit #probabilityLabel").css("background-color", "yellow");
			var value = $(this).val();
			if(value <= "3" && value != "10" ) {
				$("#riskEdit #probabilityLabel").text("Low probability");
			} 
			else if(value > "4" && value <= "6" ) {
				$("#riskEdit #probabilityLabel").text("Medium probability");
				}
			else 
				$("#riskEdit #probabilityLabel").text("High probability");
			});
		//Display when ready
		var value = $("#riskEdit #selectImpact").val();
		if(value <= "3" && value != "10" ) {
			$("#riskEdit #impactLabel").text("Low impact");
		} 
		else if(value > "4" && value <= "6" ) {
			$("#riskEdit #impactLabel").text("Medium impact");
			}
		else
			$("#riskEdit #impactLabel").text("High impact");
		
		//Display when change
		$("#riskEdit #selectImpact").change(function() {
			$("#riskEdit #impactLabel").css("background-color", "yellow");
			var value = $(this).val();
			if(value <= "3" && value != "10" ) {
				$("#riskEdit #impactLabel").text("Low impact");
			} 
			else if(value > "4" && value <= "6" ) {
				$("#riskEdit #impactLabel").text("Medium impact");
				}
			else
				$("#riskEdit #impactLabel").text("High impact");
			});
		//For adding risk entry
		//Display when ready
		var value = $("#riskAdd #selectProbability").val();
		if(value <= "3" && value != "10" ) {
			$("#riskAdd #probabilityLabel").text("Low probability");
		} 
		else if(value >= "4" && value <= "6") {
			$("#riskAdd #probabilityLabel").text("Medium probability");
			}
		else 
			$("#riskAdd #probabilityLabel").text("High probability");

		//Display when change
		$("#riskAdd #selectProbability").change(function() {
			$("#riskAdd #probabilityLabel").css("background-color", "yellow");
			var value = $(this).val();
			if(value <= "3" && value != "10" ) {
				$("#riskAdd #probabilityLabel").text("Low probability");
			} 
			else if(value >= "4" && value <= "6" ) {
				$("#riskAdd #probabilityLabel").text("Medium probability");
				}
			else 
				$("#riskAdd #probabilityLabel").text("High probability");
			});
		//Display when ready
		var value = $("#riskAdd #selectImpact").val();
		if(value <= "3" && value != "10" ) {
			$("#riskAdd #impactLabel").text("Low impact");
		} 
		else if(value >= "4" && value <= "6" ) {
			$("#riskAdd #impactLabel").text("Medium impact");
			}
		else
			$("#riskAdd #impactLabel").text("High impact");
		
		//Display when change
		$("#riskAdd #selectImpact").change(function() {
			$("#riskAdd #impactLabel").css("background-color", "yellow");
			var value = $(this).val();
			if(value <= "3" && value != "10" ) {
				$("#riskAdd #impactLabel").text("Low impact");
			} 
			else if(value >= "4" && value <= "6" ) {
				$("#riskAdd #impactLabel").text("Medium impact");
				}
			else
				$("#riskAdd #impactLabel").text("High impact");
			});

		// form process
		
		
		// Add risk
		$("form#riskAddForm").submit(function() {
			
			risk_source = $("#riskAdd .riskName").val();
			probability = $("#riskAdd #selectProbability").val();
			impact = $("#riskAdd #selectImpact").val();
			riskCost = $("#riskAdd #riskCost:checked").val();
			riskSchedule = $("#riskAdd #riskSchedule:checked").val();
			riskPerformance = $("#riskAdd #riskPerformance:checked").val();
			mitigation = $("#riskAdd textarea").val();
			
			$.ajax({
                url: '<?php echo $this->createUrl('/risks/addRisk')?>',
                type: 'POST',
                data: {
                	pid: <?php echo $pid;?>,
					risk_source: risk_source,
					probability: probability,
					impact: impact,
					riskCost: riskCost,
					riskSchedule: riskSchedule,
					riskPerformance: riskPerformance,
					mitigation: mitigation
                },
                success: function(data){
                	window.location.reload();
                },
                error: function(){
                    alert("An error occured, please try again later");
                }
                
            });
			$.fancybox.close();
			return false;
				
		});


		// get risk id
		$(".riskEdit").each(function() {
			$(this).click(function() {
				id = $(this).attr("id");
				rid = id.split('-')[1];
				$("#riskId").val(rid);

				risk_source = $("#riskContent-"+rid).text();
				$("#riskEdit .riskName").val(risk_source);
				probability = $("#probability-"+rid).text();
				$("#riskEdit #selectProbability").val(probability);
				impact = $("#impact-"+rid).text();
				$("#riskEdit #selectImpact").val(impact);
				impact_cost = $("#impact_cost-"+rid).text();
				if(impact_cost != "")
					$("#riskEdit #riskCost").attr('checked', true);
				else
					$("#riskEdit #riskCost").attr('checked', false);
				impact_schedule = $("#impact_schedule-"+rid).text();
				if(impact_schedule != "")
					$("#riskEdit #riskSchedule").attr('checked', true);
				else
					$("#riskEdit #riskSchedule").attr('checked', false);
				impact_performance = $("#impact_performance-"+rid).text();
				if(impact_performance != "")
					$("#riskEdit #riskPerformance").attr('checked', true);
				else
					$("#riskEdit #riskPerformance").attr('checked', false);
			});	
		}); 

		// Edit risk
		$("form#riskEditForm").submit(function() {
			risk_source = $("#riskEdit .riskName").val();
			probability = $("#riskEdit #selectProbability").val();
			impact = $("#riskEdit #selectImpact").val();
			riskCost = $("#riskEdit #riskCost:checked").val();
			riskSchedule = $("#riskEdit #riskSchedule:checked").val();
			riskPerformance = $("#riskEdit #riskPerformance:checked").val();
			mitigation = $("#riskEdit textarea").val();
			rid = $("#riskId").val();
			$.ajax({
                url: '<?php echo $this->createUrl('/risks/editRisk')?>',
                type: 'POST',
                data: {
                	rid: rid,
                	pid: <?php echo $pid;?>,
					risk_source: risk_source,
					probability: probability,
					impact: impact,
					riskCost: riskCost,
					riskSchedule: riskSchedule,
					riskPerformance: riskPerformance,
					mitigation: mitigation
                },
                success: function(data){
                	window.location.reload();
                },
                error: function(){
                    alert("An error occured, please try again later");
                }
                
            });
			$.fancybox.close();
			return false;
				
		});

		// Delete risk
		$(".riskDel").each(function() {
			$(this).click(function(){
                var answer = confirm ("Are you sure ?");
                if (answer == false)
                        return;
                id = $(this).attr('id');
                rid = (id.split('-'))[1];
                $.ajax({
                    url: '<?php echo $this->createUrl('/risks/delete')?>',
                    type: 'POST',
                    data: {
                        pid:<?php echo Yii::app()->request->getParam('pid')?>,
                        rid:rid
                    },
                    success: function(data){
                    	window.location.reload();
                    },
                    error: function(){
                        alert("An error occured, please try again later");
                    }
                });
                return false;
			});
		});
	});
</script>
<div class="clear"></div>
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/risk.css" />
		<div id="than">	
            <img src="<?php echo Html::imageUrl("muiten.png");?>" width="27" height="26"; style="float:left">
            <div id="textxanhdam">Risks </div>
      	</div>
        <div class="padding15"></div>
        
        <!-- Begin wrapper -->
        <div id="riskWrapper">
        	<div id="riskHeader">
        		<h2>Project Risk Register</h2>
        		<button type="button" href="#riskAdd" id="riskBtnHeader">Add a risk entry</button>
        	</div>
        	<div id="riskBody">
        		<table border="1" id="riskTable">
        			<tr>
        				<td rowspan="2" class="colorBlue riskNo">No.</td>
        				<td rowspan="2" class="colorBlue riskContent">Risk Source</td>
        				<td colspan="3" class="colorBlue">- Probability -</td>
        				<td colspan="3" class="colorBlue">- Impact -</td>
        				<td rowspan="2" class="colorBlue">Result</td>
        				<td colspan="3" class="colorBlue">- Impact Areas -</td>
        				<td rowspan="2" class="colorBlue">Mitigation Plan</td>
        			</tr>
        			<tr>
        				<td class="colorGrey 40px">Low</td>
        				<td class="colorGrey 40px">Medium</td>
        				<td class="colorGrey">High</td>
        				<td class="colorGrey">Low</td>
        				<td class="colorGrey">Medium</td>
        				<td class="colorGrey">High</td>
        				<td class="colorGrey">Cost</td>
        				<td class="colorGrey">Schedule</td>
        				<td class="colorGrey">Performance</td>
        			</tr>
        			
        			<?php
        				$riskNo = 0; 
        				foreach($risks as $risk) {
        					$riskNo = $riskNo + 1;
        			?>
        				<tr class="
        				<?php 
        					if($riskNo%2 == 0) echo 'riskOdd';
							else echo 'riskEven';	        				
        				?>">
	        				<td class="colorBlue riskNo"><?php echo $riskNo;?></td>
	        				<td class="riskContent">
	        					<div class="riskSource" id="riskContent-<?php echo $risk->risk_id;?>"><?php echo $risk->risk_source;?></div>
	        					<div class="riskControl">
	        						<a href="#riskEdit" class="riskEdit" id="riskEdit-<?php echo $risk->risk_id;?>">Edit</a>&nbsp
	        						<a href="#" class="riskDel" id="riskDel-<?php echo $risk->risk_id;?>">Delete</a>
	        					</div>
	        				</td>
	        				<?php 
	        					$probability_value = $risk->risk_probability_value;
	        					if($probability_value < 4 )
	        						echo "<td class='riskLow' id='probability-$risk->risk_id'>$probability_value</td>";
	        					else echo "<td></td>";
	        					if($probability_value <  7 && $probability_value  >= 4)
	        						echo "<td class='riskMedium' id='probability-$risk->risk_id'>$probability_value</td>";
	        					else echo "<td></td>";
	        					if($probability_value >= 7 )
	        						echo "<td class='riskHigh' id='probability-$risk->risk_id'>$probability_value</td>";
	        					else echo "<td></td>";
	        				?>
	        				<?php 
	        					$impact_value = $risk->risk_impact_value;
	        					if($impact_value < 4 )
	        						echo "<td class='riskLow' id='impact-$risk->risk_id'>$impact_value</td>";
	        					else echo "<td></td>";
	        					if($impact_value <  7 && $impact_value  >= 4)
	        						echo "<td class='riskMedium' id='impact-$risk->risk_id'>$impact_value</td>";
	        					else echo "<td></td>";
	        					if($impact_value >= 7 )
	        						echo "<td class='riskHigh' id='impact-$risk->risk_id'>$impact_value</td>";
	        					else echo "<td></td>";
	        				?>
	        				<td class="riskResult"><?php echo $probability_value*$impact_value; ?></td>
	        				<td class='riskArea' id='impact_cost-<?php echo $risk->risk_id;?>'><?php if($risk->risk_impact_cost != null) echo "X";?></td>
	        				<td class='riskArea' id='impact_schedule-<?php echo $risk->risk_id;?>'><?php if($risk->risk_impact_schedule != null) echo "X";?></td>
	        				<td class='riskArea' id='impact_performance-<?php echo $risk->risk_id;?>'><?php if($risk->risk_impact_performance != null) echo "X";?></td>
	        				<td>
	        				<?php 
	        					if($risk->risk_mitigation) 
	        						echo $risk->risk_mitigation;
	        					else
	        						echo "n/a";
	        				?>
	        				</td>
        				</tr>
        			<?php
        				 } 
        			?>
        			
        		</table>
        	</div>
        </div>
        <!-- End wrapper -->
        
   <!-- Begin edit popup -->
   <div id="riskEditPopup">
   		<div id="riskEdit" class="popupWrapper" action="" method="post">
   			<div class="popupHeader">
   				<h2>Project Risk Register - Edit Entry</h2>
   			</div>
   			<div class="popupContent">
   				<form action="" method="post" id="riskEditForm">
   					<p>Enter a risk source</p>
   					<div class="padding5"></div>
   					<input type="text" class="riskName"></input>
   					<input type="hidden" name="riskId" id="riskId"></input>
   					<div class="padding10"></div>
   					<p>Probability of risk source happending</p>
   					<div class="padding5"></div>
   					<select id="selectProbability">
   						<?php 
   						 $i = 0;
   						 for($i; $i<=10; $i++) {
   						?>
   							<option value="<?php echo $i;?>"><?php echo $i;?></option>
   						<?php 
   						 }
   						?>
   					</select>
   					<label id="probabilityLabel"></label>
   					<div class="padding10"></div>
   					<p>Impact on project of risk source happens</p>
   					<div class="padding5"></div>
   					<select id="selectImpact">
   						<?php 
   						 $i = 0;
   						 for($i; $i<=10; $i++) {
   						?>
   							<option value="<?php echo $i;?>"><?php echo $i;?></option>
   						<?php 
   						 }
   						?>
   					</select>
   					<label id="impactLabel"></label>
   					<div class="padding10"></div>
   					<p>Project Impacts</p>
   					<input type="checkbox" id="riskCost" value="1">Cost</input>
   					<input type="checkbox" id="riskSchedule" value="1">Schedule</input>
   					<input type="checkbox" id="riskPerformance" value="1">Performance</input>
					<div class="padding10"></div>
					<p>Mitigation Plan</p>
					<div class="padding5"></div>
					<textarea rows="4" cols="65"></textarea>
					<div class="postButton">
		    			<input type="submit" value="Update Risk Entry" /> or
		    			<a href="#" class="popupCancel" id="cancel">Cancel</a>
		    		</div>  					
   				</form>
   			</div>
   		</div>
   </div>
   <!-- End edit popup -->
   
   <!--  Begin add risk popup -->
	<div id="riskAddPopup">
   		<div id="riskAdd" class="popupWrapper" action="" method="post">
   			<div class="popupHeader">
   				<h2>Project Risk Register</h2>
   			</div>
   			<div class="popupContent">
   				<form action="" method="post" id="riskAddForm">
   					<p>Enter a risk source</p>
   					<div class="padding5"></div>
   					<input type="text" class="riskName" name="riskName"></input>
   					<div class="padding10"></div>
   					<p>Probability of risk source happending</p>
   					<div class="padding5"></div>
   					<select id="selectProbability">
   						<?php 
   						 $i = 0;
   						 for($i; $i<=10; $i++) {
   						?>
   							<option value="<?php echo $i;?>"><?php echo $i;?></option>
   						<?php 
   						 }
   						?>
   					</select>
   					<label id="probabilityLabel"></label>
   					<div class="padding10"></div>
   					<p>Impact on project of risk source happens</p>
   					<div class="padding5"></div>
   					<select id="selectImpact">
   						<?php 
   						 $i = 0;
   						 for($i; $i<=10; $i++) {
   						?>
   							<option value="<?php echo $i;?>"><?php echo $i;?></option>
   						<?php 
   						 }
   						?>
   					</select>
   					<label id="impactLabel"></label>
   					<div class="padding10"></div>
   					<p>Project Impacts</p>
   					<input type="checkbox" id="riskCost" value="1">Cost</input>
   					<input type="checkbox" id="riskSchedule" value="1">Schedule</input>
   					<input type="checkbox" id="riskPerformance" value="1">Performance</input>
					<div class="padding10"></div>
					<p>Mitigation Plan</p>
					<div class="padding5"></div>
					<textarea rows="4" cols="65"></textarea>
					<div class="postButton">
		    			<input type="submit" value="Add Risk Entry" /> or
		    			<a href="#" class="popupCancel" id="cancel">Cancel</a>
		    		</div>  					
   				</form>
   			</div>
   		</div>
   </div>   
   <!--  End add risk popup -->
   <?php  $this->widget('application.extensions.fancybox.EFancyBox', array( 'target'=>'a.riskEdit', 'config'=>array( 'scrolling' => 'no', 'titleShow' => false,  'hideOnOverlayClick' =>false ), ) ); ?>
   <?php  $this->widget('application.extensions.fancybox.EFancyBox', array( 'target'=>'button#riskBtnHeader', 'config'=>array( 'scrolling' => 'no', 'titleShow' => false,  'hideOnOverlayClick' =>false ), ) ); ?>